package reflect;

import java.io.File;
import java.net.URISyntaxException;
/**
 * 自动实例化与当前类Test3在同一个包中被@AutoRunClass标注的类,并将该对象输出到
 * 控制台上.
 */
public class Test3 {
    public static void main(String[] args) throws URISyntaxException, ClassNotFoundException {
        File dir = new File(Test3.class.getResource(".").toURI());
        File[] subs = dir.listFiles(f->f.getName().endsWith(".class"));
        for(File file : subs){
            String fileName = file.getName();
            String className = fileName.substring(0,fileName.indexOf("."));
            Class cls = Class.forName(Test3.class.getPackage().getName()+"."+className);
            if (cls.isAnnotationPresent(AutoRunClass.class)){
                System.out.println("被@AutoRunClass标注的类:"+className);
            }
        }
    }
}
